

package com.maoshi.shop.multishop.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.model.AttachFile;
import com.maoshi.shop.common.bean.Qiniu;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.AttachFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * 文件上传 controller
 * @author lgh
 *
 */
@RestController
@RequestMapping("/admin/file")
public class FileController {

	@Autowired
	private AttachFileService attachFileService;
	@Autowired
	private Qiniu qiniu;

	@PostMapping("/upload/element")
	public ResponseEntity<String> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ResponseEntity.noContent().build();
        }
		String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());
        return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/img")
	public ResponseEntity<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		Long shopId = SecurityUtils.getShopUser().getShopId();
		AttachFile attachFile = new AttachFile();
		attachFile.setFileType(FileUtil.extName(file.getOriginalFilename()));
		attachFile.setFileName(FileUtil.mainName(file.getOriginalFilename()));
		attachFile.setType(1);
		attachFile.setShopId(shopId);
		attachFile.setUploadTime(new Date());
		String fileName = attachFileService.uploadImg(file.getBytes(),attachFile);
		return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/tinymceEditor")
	public ResponseEntity<String> uploadTinymceEditorImages(@RequestParam("editorFile") MultipartFile editorFile) throws IOException{
		String fileName =  attachFileService.uploadFile(editorFile.getBytes(),editorFile.getOriginalFilename());
        return ResponseEntity.ok(qiniu.getResourcesUrl() + fileName);
	}

	/**
	 * 分页获取历史图片
	 */
	@GetMapping("/attachFilePage")
	public ResponseEntity<IPage<AttachFile>> attachFilePage(PageParam<AttachFile> page, AttachFile attachFile) {
		Long shopId = SecurityUtils.getShopUser().getShopId();
		IPage<AttachFile> attachFilePage = attachFileService.page(page,new LambdaQueryWrapper<AttachFile>()
				.eq(AttachFile::getShopId,shopId)
				.eq(AttachFile::getType,1)
				.like(StrUtil.isNotBlank(attachFile.getFileName()),AttachFile::getFileName,attachFile.getFileName())
				.orderByDesc(AttachFile::getUploadTime));
		return ResponseEntity.ok(attachFilePage);
	}


}
